package junit;

import org.junit.*;

import fast.Buffer;
import fast.TCPListener;
import fast.TCPTalker;

import static org.junit.Assert.* ;

public class TCPTest {
	
	private Buffer talkBuffer;
	private Buffer listenBuffer;
	private Thread talk;
	private Thread listen;
	
	@Test
	public void test_send(){
		
		System.out.println("Add 4 talk buffer entries");
		
		//add some records to the talk buffer
		try {
			talkBuffer.add("127.0.0.1", 10001, 1, "search", "foo1");
			talkBuffer.add("127.0.0.1", 10001, 2, "search", "foo2");
			talkBuffer.add("127.0.0.1|10001|3|search|foo3");
			talkBuffer.add("127.0.0.1|10001|4|search|foo4");
		} catch (InterruptedException e) {
			System.out.print(e);
		}
				
		//print out the contents of the listenBuffer
		for(int i=0; i<4; i++){
			try {
				System.out.println(listenBuffer.remove());
			} catch (InterruptedException e) {
				System.out.print(e);
			}//try
		}//for
	}
	
	@Before public void load_state(){
		talkBuffer = new Buffer(10);
		listenBuffer = new Buffer(10);
		
		//start the listener and talker threads	
		listen = new Thread(new TCPListener(10001, listenBuffer));
		talk = new Thread(new TCPTalker(talkBuffer));
		
		listen.start();
		talk.start();
	}
	
	@After public void unload_state(){
		listen = null;
		talk = null;
	}
	
}
